package com.dh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dh.common.ResultMapUtil;
import com.dh.pojo.Supplier;
import com.dh.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 供应商相关的控制层
 */
@Controller
@RequestMapping(value = "/supplier") //设置公共的返回地址
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 1.转向供应商页面
     */
    @RequestMapping
    public String supplier(){
        return "/supplier";
    }

    /**
     *
     * 2.分页查询供应商列表
     * @param param 查询参数 供应商名字
     * @param pageNum 当前页面是第几页
     * @param pageSize 每一页的大小
     * @return
     */
    @RequestMapping(value = "/supplierQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "10")int pageSize){
        try{
            //去service层查询参数配置
            IPage<Supplier> iPage = supplierService.selectSupplierPage(pageNum,pageSize,param);
            //5.封装到方法中
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){//返回错误的结果
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 6.转向供应商新增页面
     */
    @RequestMapping(value = "/supplierPage")
    public String supplierPage(){
        return "/supplierPage";
    }

    /**
     * 7.添加一个供应商
     * @return
     */
    @RequestMapping(value = "/supplierAdd")
    @ResponseBody
    public Object supplierAdd(Supplier supplier){
        try{
        //获取当前时间
        supplier.setCreatetime(new Date());
        //执行保存
        int i = supplierService.addSupplier(supplier);
        return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 9.转向供应商编辑页面
     * 根据id查询，required 必须存在
     */
    @RequestMapping(value = "/supplierQueryById")
    public String supplierQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        //根据Id 获取查询当前对象
        Supplier supplier = supplierService.querySupplierById(id);
        //把查询的数据，存入model中
        model.addAttribute("obj",supplier);
        return "/supplierPage";
    }

    /**
     * 10.修改一个供应商
     */
    @RequestMapping(value = "/supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){
        try{
            int i = supplierService.updateSupplier(supplier);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){//失败就报异常
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 11.删除一个供应商
     */
    @RequestMapping(value = "/supplierDelById")
    @ResponseBody
    public Object supplierDelById(Integer id){
        try{
            int i = supplierService.DeleteSupplierById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有供应商
     */
    @RequestMapping(value = "/supplierList")
    @ResponseBody
    public Object supplierList(){
        List<Supplier> supplierList = supplierService.querySupplierList();
        return ResultMapUtil.getHashMapList(supplierList);
    }
}
